package com.zouxian.pipeline;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.annotation.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.utils.FilePersistentBase;
@ThreadSafe
public class MyFilePipeline extends FilePersistentBase implements Pipeline {

	 private Logger logger = LoggerFactory.getLogger(getClass());

	    /**
	     * create a FilePipeline with default path"/data/webmagic/"
	     */
	    public MyFilePipeline() {
	        setPath("d:/data/webmagic/");
	    }

	    public MyFilePipeline(String path) {
	        setPath(path);
	    }

	    public void process(ResultItems resultItems, Task task) {
	        String path = this.path + PATH_SEPERATOR + task.getUUID() + PATH_SEPERATOR;
	        try {
	        	SimpleDateFormat sd=new SimpleDateFormat("yyyyMMDD");
	        	String data = sd.format(new Date());
	            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(getFile(path + data+"_"+DigestUtils.md5Hex(resultItems.getRequest().getUrl()) + ".txt")),"UTF-8"));
	            //printWriter.println("url:" + resultItems.getRequest().getUrl());
	            for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
	            	
	                if (entry.getValue() instanceof Iterable) {
	                    Iterable value = (Iterable) entry.getValue();
	                    printWriter.println(entry.getKey() + ":");
	                    for (Object o : value) {
	                        printWriter.println(o);
	                    }
	                } else {
	                	if(entry.getValue()==null){
	                		printWriter.println("["+entry.getKey() + "]:");
	                	}else{
	                		printWriter.println("["+entry.getKey() + "]:" + entry.getValue());
	                	}
	                }
	            }
	            printWriter.close();
	        } catch (IOException e) {
	            logger.warn("write file error", e);
	        }
	    }
}
